//
//  MFewFetureViewController.m
//  MFLive_iOS
//
//  Created by 邓建 on 2019/4/30.
//  Copyright © 2019 邓建. All rights reserved.
//

#import "MFewFetureViewController.h"
#import "MFNewFetureViewCell.h"

@interface MFewFetureViewController ()
@property (nonatomic,strong) UIPageControl *control; //pageController

@end

@implementation MFewFetureViewController


static NSString *ID = @"Cell";

- (instancetype)init
{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    // 设置cell的尺寸
    layout.itemSize = [UIScreen mainScreen].bounds.size;
    
    // 清空行距
    layout.minimumLineSpacing = 0;
    
    // 设置滚动的方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    return [super initWithCollectionViewLayout:layout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 注册cell,默认就会创建这个类型的cell
    [self.collectionView registerClass:[MFNewFetureViewCell class] forCellWithReuseIdentifier:ID];
    // 分页
    self.collectionView.pagingEnabled = YES;
    self.collectionView.bounces = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.backgroundColor=[UIColor whiteColor];
    
    // 添加pageController
    [self setUpPageControl];
}

// 添加pageController
- (void)setUpPageControl
{
    // 添加pageController,只需要设置位置，不需要管理尺寸
    UIPageControl *control = [[UIPageControl alloc] init];
    
    control.numberOfPages = 4;
    control.pageIndicatorTintColor = [UIColor lightGrayColor]; //设置当滑动的颜色
    control.currentPageIndicatorTintColor =[UIColor blackColor]; // 设置当前页的颜色
    
    // 设置center
    control.center = CGPointMake(self.view.width * 0.5, self.view.height
                                 -60);
    _control = control;
    [self.view addSubview:control];

}

#pragma mark - UIScrollView代理
// 只要一滚动就会调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 获取当前的偏移量，计算当前第几页
    int page = scrollView.contentOffset.x / scrollView.bounds.size.width + 0.5;
    
    //隐藏最后一页
    
    _control.hidden=page==3?YES:NO;
    // 设置页数
    _control.currentPage = page;
}
#pragma mark - UICollectionView代理和数据源
// 返回有多少组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
// 返回第section组有多少个cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}



// 返回cell长什么样子
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
  
    MFNewFetureViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    NSString *imageName = [NSString stringWithFormat:@"%ld.jpg",(long)indexPath.item+1 ];
    cell.image = [UIImage imageNamed:imageName];
    [cell setIndexPath:indexPath count:3];
    
    return cell;
    
}

@end
